/*
 * Created on Sep 23, 2003
 * Approve
 * Local Projects Database
 * (comparator) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.usr.messaging;

import java.util.Comparator;

import ro.gateway.aida.obj.GenericActivityProperty;
import ro.gateway.aida.usr.Arbiter;


/**
 * @author Mihai Postelnicu
 * Approve
 *
 *  *
 */
public class Approve extends Alert {
    protected String lang;
    protected GenericActivityProperty title;
    protected String reason;
    protected boolean approved;
    protected boolean show;
    protected int feedback; //0-poor, consider revision

    public Approve(Event l, String lang) {
        this.lang = lang;
        setSourceUserId(l.getSourceUserId());
        setEventId(l.getEventId());
        setSourceObjId(l.getSourceObjId());
        setSent(l.getSent());
        setSourceType(l.getSourceType());
        setEventType(l.getEventType());
        setSourceUserName(l.getSourceUserName());
        show = true;
    }

    public Approve() {
        super();
        show = true;
    }

    public int getState() {
        if (approved && (read != null))
            return 2;

        if (!approved && (read != null))
            return 1;

        return 0;
    }

    public String getName() {
        StringBuffer sb = new StringBuffer();
        int s = getState();

        if (s == 2)
            sb.append("Approved ");
        else if (s == 1)
            sb.append("Rejected ");
        else
            sb.append("Unapproved ");

        switch (sourceType) {
        case Arbiter.PROJECT_TYPE:
            sb.append("project ");

            break;

        case Arbiter.ORG_TYPE:
            sb.append("organization ");

            break;

        case Arbiter.PERSON_TYPE:
            sb.append("person ");

            break;

        default:
            sb.append("object ");
        }

        return sb.toString();
    }

    public boolean isApproved() {
        return approved;
    }

    /**
     * @return
     */
    public int getFeedback() {
        return feedback;
    }

    /**
     * @return
     */
    public String getReason() {
        return reason;
    }

    /**
     * @param b
     */
    public void setApproved(boolean b) {
        approved = b;
    }

    /**
     * @param i
     */
    public void setFeedback(int i) {
        feedback = i;
    }

    /**
     * @param string
     */
    public void setReason(String string) {
        reason = string;
    }

    /**
     * @return
     */
    public String getTitleString() {
        return (title == null) ? "[N/A]" : title.getName(lang);
    }

    /**
     * @param property
     */
    public void setTitle(GenericActivityProperty property) {
        title = property;
    }

    /**
     * @return
     */
    public String getLang() {
        return lang;
    }

    /**
     * @param string
     */
    public void setLang(String string) {
        lang = string;
    }

    /**
     * @return
     */
    public boolean isShow() {
        return show;
    }

    /**
     * @param b
     */
    public void setShow(boolean b) {
        show = b;
    }

    public static class NameComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Approve t1 = (Approve) o1;
            Approve t2 = (Approve) o2;

            return (t1.getTitleString().compareTo(t2.getTitleString()));
        }
    }

    public static class TypeComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Approve t1 = (Approve) o1;
            Approve t2 = (Approve) o2;
            int x1;
            int x2;

            if (t1.isApproved())
                x1 = 0;
            else if (t1.getRead() != null)
                x1 = -1;
            else
                x1 = 1;

            if (t2.isApproved())
                x2 = 0;
            else if (t2.getRead() != null)
                x2 = -1;
            else
                x2 = 1;

            return x1 - x2;
        }
    }

    public static class UserNameComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Approve t1 = (Approve) o1;
            Approve t2 = (Approve) o2;

            return (t1.getSourceUserName().compareTo(t2.getSourceUserName()));
        }
    }

    public static class ReadDateComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Approve t1 = (Approve) o1;
            Approve t2 = (Approve) o2;

            if ((t1.getRead() == null) && (t2.getRead() == null))
                return 0;

            if ((t1.getRead() == null) && (t2.getRead() != null))
                return 1;

            if ((t2.getRead() == null) && (t1.getRead() != null))
                return -1;

            return (t1.getRead().compareTo(t2.getRead()));
        }
    }

    public static class IssueDateComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Approve t1 = (Approve) o1;
            Approve t2 = (Approve) o2;

            if ((t1.getSent() == null) && (t2.getSent() == null))
                return 0;

            if ((t1.getSent() == null) && (t2.getSent() != null))
                return 1;

            if ((t2.getSent() == null) && (t1.getSent() != null))
                return -1;

            return (t1.getSent().compareTo(t2.getSent()));
        }
    }

	public static final int ALL = 0;
    public static final int APPROVED = 1;
    public static final int REJECTED = 2;
    public static final int UNAPPROVED = 3;
    public static final String LIST="Approvals_LIST";
}
